package com.example.dell.widget_0503guojiuqi;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button button;
    EditText editText;
    TextView textView;
    ImageView imageView;
    ImageButton imagebutton;
    Button button2;
    Button button3;
    RadioButton radioButton_m,radioButton_f;
    CheckBox checkBox,checkBox2,checkBox3;
    RadioGroup radioGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imagebutton=(ImageButton)findViewById(R.id.imageButton);
        radioGroup=(RadioGroup) findViewById(R.id.radioGroup);
        button=(Button)findViewById(R.id.button);
        editText=(EditText)findViewById(R.id.editText);
        textView=(TextView)findViewById(R.id.textView);
        button2=(Button)findViewById(R.id.button2);
        imageView=(ImageView)findViewById(R.id.imageView);
        button3=(Button)findViewById(R.id.button3);
        radioButton_f=(RadioButton)findViewById(R.id.radioButton_f);
        radioButton_m=(RadioButton)findViewById(R.id.radioButton_m);
        checkBox=(CheckBox)findViewById(R.id.checkBox);
        checkBox2=(CheckBox)findViewById(R.id.checkBox2);
        checkBox3=(CheckBox)findViewById(R.id.checkBox3);

        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId==R.id.radioButton_f)
                    Toast.makeText(MainActivity.this,"female",Toast.LENGTH_SHORT).show();

                else if (checkedId==R.id.radioButton_m)
                    Toast.makeText(MainActivity.this,"male",Toast.LENGTH_SHORT).show();
            }
        });
        //选中状态改变后就会执行该方法
        radioButton_m.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView==radioButton_m && isChecked){
                    Toast.makeText(MainActivity.this,"radiobutton is checked",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        System.out.println("==>>"+"in onClick()");
        switch (v.getId()){
            case R.id.button:
                String text=editText.getText().toString();
                textView.setText(text);
                Toast.makeText(MainActivity.this,"button is clicked",Toast.LENGTH_SHORT).show();
                break;
            case R.id.button2:
                Toast.makeText(MainActivity.this,"imageView",Toast.LENGTH_SHORT).show();
                imageView.setImageResource(R.mipmap.img_1);
                break;
            case  R.id.button3:
                String text1="用户选择是";
                if (radioButton_f.isChecked())
                    text1+=radioButton_f.getText();
                if (radioButton_m.isChecked())
                    text1+=radioButton_m.getText();

                if (checkBox.isChecked())
                    text1+=" "+checkBox.getText();
                if (checkBox2.isChecked())
                    text1+=" "+checkBox2.getText();
                if (checkBox3.isChecked())
                    text1+=" "+checkBox3.getText();

                Toast.makeText(MainActivity.this,text1,Toast.LENGTH_SHORT).show();
                break;
        }

    }
}
